﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Uhul.Controls
{
  
  /// <summary>
  /// MapPin that points to POI on map. Control is inherited from button and adds Dependency property GradientColor that easies
  /// different pin colors in map
  /// </summary>
  //[TemplatePart(Name = mapPinButtonLabel, Type = typeof(TextBlock))]
  [TemplatePart(Name = gradientPinShape, Type = typeof(Shape))]
  public class MapPinButton : Button
  {

    //private const string mapPinButtonLabel = "MapPinButtonLabel";
    private const string gradientPinShape = "GradientPinShape";

    //controls
    //private TextBlock _pinButtonLabel;
    private Shape _pinButtonShape;



    public MapPinButton()
    {
      this.DefaultStyleKey = typeof(MapPinButton);
    }




    public override void OnApplyTemplate()
    {
      base.OnApplyTemplate();

      _pinButtonShape = GetTemplateChild(gradientPinShape) as Shape;

      _resolveVisualState();
    }





    /// <summary>
    /// PinCharacterProperty dependency property
    /// </summary>
    public static readonly DependencyProperty GradientColorProperty = DependencyProperty.Register(
        "GradientColor", typeof(object), typeof(MapPinButton), new PropertyMetadata(OnGradientColorPropertyChanged));

    /// <summary>
    /// Gets or sets a value indicating text displayed inside of map pin.  This is a dependency property.
    /// </summary>
    /// <value></value>
    /// <returns></returns>
    public Color GradientColor
    {
      get
      {
        object tmp = GetValue(GradientColorProperty);

        if (!(tmp is Color))
          return Colors.Black;
        else
          return (Color)GetValue(GradientColorProperty); 
      }
      set { SetValue(GradientColorProperty, value); }
    }


    private static void OnGradientColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      if (e.NewValue != null)
        if (!(e.NewValue is Color) && e.NewValue is String)
          if (((string)e.NewValue).Length == 9)
          {
            string s = e.NewValue as string;
            //try convert object to Color
            byte a = System.Convert.ToByte(s.Substring(0, 2), 16);
            byte r = System.Convert.ToByte(s.Substring(2, 2), 16);
            byte g = System.Convert.ToByte(s.Substring(4, 2), 16);
            byte b = System.Convert.ToByte(s.Substring(6, 2), 16);

            Color tmp = Color.FromArgb(a, r, g, b);
            MapPinButton m = (MapPinButton)d;
            m.GradientColor = tmp;
          }

    }





    private void _resolveVisualState()
    {

      if(_pinButtonShape == null)
        return;

      if (GradientColor == null)
      {
        _pinButtonShape.Opacity = 0;
        return;
      }

      double light = 1.4;
      double dark = 0.6;

      Color lightColor;
      Color darkColor;

      Color tmp = GradientColor;

      lightColor = Color.FromArgb(tmp.A, (byte)(tmp.R * light), (byte)(tmp.G * light), (byte)(tmp.B * light));
      darkColor = Color.FromArgb(tmp.A, (byte)(tmp.R * dark), (byte)(tmp.G * dark), (byte)(tmp.B * dark));

      GradientStopCollection g = new GradientStopCollection();
      g.Add(
        new GradientStop(){
        Color = lightColor,
        Offset = 1
        }
      );
      g.Add(
        new GradientStop(){
        Color = darkColor,
        Offset = 0
        }
      );

      _pinButtonShape.Fill = new LinearGradientBrush()
      {
        StartPoint = new Point(0.5,0),
        EndPoint = new Point(0.5,1),
        GradientStops = g
      };

      _pinButtonShape.Stroke = new SolidColorBrush(Color.FromArgb(200, tmp.R, tmp.G, tmp.B));
      _pinButtonShape.Opacity = 1;

    }



  }

}
